package com.example.taxfloatproject20240926.ui.utils;

import android.content.Context;
import android.media.MediaPlayer;

/**
 * Created by gf on 15-1-22.
 */
public class PlaySound {

    private static MediaPlayer currentMediaPlayer = null;

    public static void play(Context context, int resId, MediaPlayer.OnCompletionListener listener) {
        try {
            if (null != currentMediaPlayer) {
                currentMediaPlayer.stop();
                currentMediaPlayer.release();
                currentMediaPlayer = null;
            }
            currentMediaPlayer = MediaPlayer.create(context, resId);
            currentMediaPlayer.setLooping(true);
            if (null != currentMediaPlayer) {
                currentMediaPlayer.start();
                currentMediaPlayer.setOnCompletionListener(listener);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void stopAudio() {
        if (currentMediaPlayer != null) {
            currentMediaPlayer.pause();
            currentMediaPlayer = null;
        }
    }

}
